<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2>企业登录</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginForm">
        <el-form-item prop="username" label="用户名">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin">登录</el-button>
          <el-button @click="$router.push('/enterprise/register')">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'EnterpriseLogin',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async handleLogin() {
      try {
        const valid = await this.$refs.loginForm.validate()
        if (valid) {
          const res = await this.$axios.post('/sysUser/login', this.loginForm)
          
          if (res.data.code === 200) {
            localStorage.setItem('token', res.data.data)
            localStorage.setItem('username', this.loginForm.username)
            
            this.$message.success('登录成功')
            
            this.$emit('login-success')
            
            const redirect = this.$route.query.redirect || '/jobs'
            this.$router.push(redirect)
          } else {
            this.$message.error(res.data.msg || '账号或密码错误')
          }
        }
      } catch (error) {
        console.error('登录失败:', error)
        this.$message.error(error.response?.data?.msg || error.message || '登录失败')
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-card {
  width: 400px;
}
</style> 